<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\1\6 0006
 * Time: 16:23
 */
namespace app\admin\controller;
use app\admin\model\Version as VersionModel;

class Version extends UserAuth {
    /**
     * 版本管理
     * @author 王柯 <wangke.email@foxmail.com>
     * @return mixed
     */
    public function index(){
        
        return $this->fetch('index');
    }

    /**
     * app版本信息列表
     * @author 王柯 <wangke.email@foxmail.com>
     * @return \think\response\View
     */
    public function version_list(){
        if(request()->isGet()){
            $keywords = trim(input('get.keywords'));
            $where = [
                'app_type|version|version_code' => ['like', "%{$keywords}%"]
            ];
            if(empty($keywords)){
                $version = (new VersionModel())
                    ->getAll();
                $count = (new VersionModel())->getCount();
            }else{
                $version = (new VersionModel)
                    ->getAll($where);
                $count = (new VersionModel())->getCount($where);
            }

            $this->assign(
                [
                    'count' => $count,
                    'version' => $version,
                ]
            );
        }
        
        return view('version_list');
    }

    /**
     * 新增版本信息
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function add_version(){
        if(request()->isAjax()){
            $data = $_POST;
            $add = (new VersionModel())
                ->allowField(true)
                ->save($data);
            if($add){
                return show(config('code.success'),'添加成功！');
            }else{
                return show(config('code.error'),'添加失败！');
            }
        }

        return view('add_version');
    }

    /**
     * 修改版本信息
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function edit_version($id){
        $version = (new VersionModel())->getOne($id);
        if(request()->isAjax()){
            $data = $_POST;
            $time['update_time'] = time();
            $update = (new VersionModel())
                ->where('id='.$data['id'])
                ->update($data);
            if($update){
                (new VersionModel())
                    ->where('id='.$data['id'])
                    ->update($time);
                return show(config('code.success'),'修改成功！');
            }else{
                return show(config('code.error'),'修改失败！');
            }
        }

        $this->assign('version',$version);
        return view('edit_version');
    }

    /**
     * app版本信息详情
     * @author 王柯 <wangke.email@foxmail.com>
     * @return \think\response\View
     */
    public function version_show($id){
        $version = (new VersionModel())
            ->getOne($id);
        $this->assign('version',$version);
        return view('version_show');
    }

    /**
     * 删除版本信息
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function del_version(){
        $data = $_POST;
        if(!isset($data['id'])){
            return show(config('code.error'),'参数id错误！');
        }

        $team = new VersionModel();
        $res = $team
            ->where('id='.$data['id'])
            ->delete();

        if($res){
            return show(config('code.success'),'删除成功！');
        }else{
            return show(config('code.error'),'删除失败！');
        }
    }

    /**
     * 批量删除版本信息
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function batch_del_version(){
        $data = $_POST;
        if(!isset($data['id'])){
            return show(config('code.error','参数id错误！'));
        }
        $arr_id = $data['id'];
        $str = implode(',', $arr_id);

        $del = (new VersionModel())
            ->where('id', 'in', $str)
            ->delete();

        if ($del) {
            return show(config('code.success'), '删除成功！');
        } else {
            return show(config('code.error'), '删除失败！');
        }
    }

}